package activiti2;

import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngines;
import org.activiti.engine.history.HistoricIdentityLink;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.IdentityLink;
import org.activiti.engine.task.Task;
import org.junit.Test;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 分配组任务
 */
public class FenPeiZuRenWu {

    ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();

    /**
     * 部署流程定义+启动流程实例
     * 部署ID：87501
     * 流程实例ID：87505
     * 流程定义的ID：myProcess_8:4:87504
     *
     */
    @Test
    public void deployementAndStartProcess(){
        Deployment deployment = processEngine.getRepositoryService()
                .createDeployment()
                .addClasspathResource("diagrams/fenpeizurenwu.bpmn")
                .deploy();
        System.out.println("部署ID：" + deployment.getId());

        Map<String,Object> variables = new HashMap<>();
        variables.put("userIDs","AA,BB,CC");
        ProcessInstance pi = processEngine.getRuntimeService()
                .startProcessInstanceByKey("myProcess_8",variables);

        System.out.println("流程实例ID："+pi.getId());
        System.out.println("流程定义的ID："+pi.getProcessDefinitionId());
    }


    /**
     * 查询我的个人任务
     *
     *
     */
    @Test
    public void findPersonalTaskList(){
        String assignee = "AA";
        List<Task> list = processEngine.getTaskService()
                .createTaskQuery()
                .taskAssignee(assignee)
                .list();
        if(list!=null && list.size()>0){
            for(Task task:list){
                System.out.println("任务的办理人："+task.getAssignee());
                System.out.println("任务名称："+task.getName());
                System.out.println("任务ID："+task.getId());
                System.out.println("任务的创建时间："+task.getCreateTime());
                System.out.println("流程实例ID："+task.getProcessInstanceId());
                System.out.println("#######################################");
            }
        }
    }

    /**
     * 查询组任务
     *
     *
     * */
    @Test
    public void findGroupTaskList(){
        String candidateUser = "AA";
        List<Task> list = processEngine.getTaskService()
                .createTaskQuery()
                .taskCandidateUser(candidateUser)
                .list();

        if(list!=null && list.size()>0){
            for(Task task:list){
                System.out.println("任务的办理人："+task.getAssignee());
                System.out.println("任务名称："+task.getName());
                System.out.println("任务的创建时间："+task.getCreateTime());
                System.out.println("任务ID："+task.getId());
                System.out.println("流程实例ID："+task.getProcessInstanceId());
                System.out.println("#######################################");
            }
        }

    }

    /**
     *
     * 查询正在执行的组任务列表
     *
     * */
    @Test
    public void findGroupCandidate(){
        String taskId = "82509";
        List<IdentityLink> list = processEngine.getTaskService()
                .getIdentityLinksForTask(taskId);
        if(list!=null && list.size()>0){
            for(IdentityLink identityLink:list){
                System.out.println("任务ID："+identityLink.getTaskId());
                System.out.println("流程实例ID："+identityLink.getProcessInstanceId());
                System.out.println("用户ID："+identityLink.getUserId());
                System.out.println("工作流角色ID："+identityLink.getGroupId());
                System.out.println("#########################################");
            }
        }

    }

    /**完成任务*/
    @Test
    public void completeTask(){
        //任务ID
        String taskId = "82509";
        processEngine.getTaskService().complete(taskId);
        System.out.println("完成任务："+taskId);
    }

    /**
     *
     * 查询历史的组任务列表
     *
     * */
    @Test
    public void findHistoryGroupCandidate(){
        //流程实例ID
        String processInstanceId  = "80005";
        List<HistoricIdentityLink> list = processEngine.getHistoryService()
                .getHistoricIdentityLinksForProcessInstance(processInstanceId);
        if(list!=null && list.size()>0){
            for(HistoricIdentityLink identityLink:list){
                System.out.println("流程实例ID："+identityLink.getProcessInstanceId());
                System.out.println("用户ID："+identityLink.getUserId());
                System.out.println("工作流角色ID："+identityLink.getGroupId());
                System.out.println("任务ID："+identityLink.getTaskId());
                System.out.println("#########################################");
            }
        }

    }



}
